package main

import (
	_ "quickstart/routers"
	"github.com/astaxie/beego"
	"quickstart/models"
	"github.com/astaxie/beego/orm"
)

func inserUser() {
	o := orm.NewOrm()
	user := models.User{}
	//user.Id = "1"
	user.Name = "wyj"
	id,err := o.Insert(&user)
	if err != nil{
		beego.Info("insert erro")
		return
	}
	beego.Info("insert success ,id = ",id)
}

func queryUser(){
	o := orm.NewOrm()
	user := models.User{Id:1}
	err := o.Read(&user)
	if err != nil{
		beego.Info("query is erro")
		return
	}
	beego.Info("query success , user = ",user)
}

func userUpdate(){
	o := orm.NewOrm()
	user := models.User{Id:1,Name:"haha"}
	_,err := o.Update(&user)
	if err != nil{
		beego.Info("update err")
		return
	}
	beego.Info("update success")
}

func deleteUser(){
	o := orm.NewOrm()
	user := models.User{Id:2}
	_,err := o.Delete(&user)
	if err != nil{
		beego.Error("delete err")
		return
	}
	beego.Error("delete success")
}

func insertorder() {
	o := orm.NewOrm()
	order := models.User_order{}
	order.Order_data = "this is order"
	user := models.User{Id:1}
	order.User = &user
	id,err := o.Insert(&order)
	if err != nil{
		beego.Info("inser erro")
		return
	}
	beego.Info("insert success,id = ",id)
}

func queryOrder(){
	var orders []*models.User_order
	o := orm.NewOrm()
	qs := o.QueryTable("User_order")
	order_num,err := qs.Filter("user__id",1).All(&orders)  // user.id
	if err != nil{
		beego.Error("query erro")
		return
	}
	beego.Info("query order_num = ",order_num)
	for _,order := range orders{
		beego.Info(" query order success,order = ",order)
	}
}

func main() {
	//inserUser()
	//queryUser()
	//userUpdate()
	//deleteUser()
	//insertorder()
	queryOrder()
	beego.SetStaticPath("download1","down")
	beego.Run()
}

